
package com.jojoteamwork.photo.ui;

import com.jojoteamwork.photo.R;
import com.jojoteamwork.photo.provider.Model;
import com.jojoteamwork.photo.util.ImageWorker;
import com.jojoteamwork.photo.util.Utils;
import com.jojoteamwork.photo.util.ZipDataReader;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ModelDetailFragment extends Fragment {
    private static final String EXTRA_MODEL_DATA = "model";

    private static final String EXTRA_FILE_NAME = "filename";

    private Model mModel;

    private String mFileName;

    private ImageWorker mImageWorker;

    private TextView tvTitle;

    private TextView tvActress;

    private TextView tvIntroduction;

    private Button btnDetailShow;

    private Button btnDetailHidden;

    private Button btnSavePhoto;

    private ImageView ivFrontcover;

    FrameLayout layoutInfo;

    private class ViewHold {
        Button btnDetailShow;

        Button btnDetailHidden;

        TextView tvIntroduction;

    }

    public static ModelDetailFragment newInstance(Model model, String filename) {
        final ModelDetailFragment f = new ModelDetailFragment();

        final Bundle args = new Bundle();
        args.putSerializable(EXTRA_MODEL_DATA, model);
        args.putString(EXTRA_FILE_NAME, filename);
        f.setArguments(args);

        return f;
    }

    public ModelDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        mModel = (Model) getArguments().getSerializable(EXTRA_MODEL_DATA);
        mFileName = getArguments().getString(EXTRA_FILE_NAME);
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.detail_item, container, false);
        tvTitle = (TextView) view.findViewById(R.id.title);
        tvActress = (TextView) view.findViewById(R.id.actress);
        tvIntroduction = (TextView) view.findViewById(R.id.introduction);
        ivFrontcover = (ImageView) view.findViewById(R.id.frontcover);
        btnDetailShow = (Button) view.findViewById(R.id.btnDetailShow);
        btnDetailHidden = (Button) view.findViewById(R.id.btnDetailHidden);
        btnSavePhoto = ((Button) view.findViewById(R.id.btnDownload));
        ViewHold vh = new ViewHold();
        vh.btnDetailHidden = btnDetailHidden;
        vh.btnDetailShow = btnDetailShow;
        vh.tvIntroduction = tvIntroduction;
        vh.btnDetailHidden.setTag(vh);
        vh.btnDetailShow.setTag(vh);
        layoutInfo = (FrameLayout) view.findViewById(R.id.layoutInfo);
        layoutInfo.setTag(vh);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        mImageWorker = ((ModelDetailActivity) getActivity()).getImageWorker();
        setInfoView(mModel);
        super.onActivityCreated(savedInstanceState);
    }

    private void setInfoView(final Model model) {
        tvTitle.setText(model.name);

        tvActress.setText(model.introduction);
        tvIntroduction.setText(model.detail);
        String urls = Utils.MAGAZINE_DIR + "/" + mFileName + ";" + model.cover;

        mImageWorker.loadImage(urls, ivFrontcover);
        // ivFrontcover.setTag(model);

        ivFrontcover.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // Model model = (Model) v.getTag();
                Intent intent = new Intent(getActivity(), PhotoBrowseActivity.class);
                intent.putExtra(EXTRA_MODEL_DATA, model);
                intent.putExtra(EXTRA_FILE_NAME, mFileName);
                startActivity(intent);
            }
        });
        layoutInfo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ViewHold vh = (ViewHold) v.getTag();
                int visibility = vh.btnDetailShow.getVisibility();
                switch (visibility) {
                    case View.VISIBLE:
                        vh.tvIntroduction.setVisibility(View.VISIBLE);
                        vh.btnDetailHidden.setVisibility(View.VISIBLE);
                        vh.btnDetailShow.setVisibility(View.GONE);
                        break;
                    case View.GONE:
                        vh.tvIntroduction.setVisibility(View.GONE);
                        vh.btnDetailHidden.setVisibility(View.GONE);
                        vh.btnDetailShow.setVisibility(View.VISIBLE);
                        break;
                }
            }

        });
        btnDetailShow.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ViewHold vh = (ViewHold) v.getTag();
                vh.tvIntroduction.setVisibility(View.VISIBLE);
                vh.btnDetailHidden.setVisibility(View.VISIBLE);
                vh.btnDetailShow.setVisibility(View.GONE);
            }
        });

        btnDetailHidden.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ViewHold vh = (ViewHold) v.getTag();
                vh.tvIntroduction.setVisibility(View.GONE);
                vh.btnDetailHidden.setVisibility(View.GONE);
                vh.btnDetailShow.setVisibility(View.VISIBLE);
            }
        });
        btnSavePhoto.setTag(model);
        btnSavePhoto.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Utils.showDefaultAlertDialog(getActivity(), getString(R.string.save_title),
                        getString(R.string.save_message), new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SaveTask task = new SaveTask();
                                task.execute(new String[] {
                                        model.pic, Utils.PHOTOPATH + model.name + "/"
                                });

                            }
                        });

                // savePhoto((Model) v.getTag());
            }
        });
    }

    class SaveTask extends AsyncTask<String, Void, Void> {
        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(getActivity());
            dialog.setCancelable(false);
            dialog.setMessage(getString(R.string.saving));
            dialog.setCanceledOnTouchOutside(false);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.show();
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(String... params) {
            String srcDirPath = params[0];
            String destDirPath = params[1];
            ZipDataReader.copyDircetoryFromZip(getActivity(), Utils.MAGAZINE_DIR + "/" + mFileName,
                    srcDirPath, destDirPath);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            dialog.dismiss();
            Toast.makeText(getActivity(), R.string.saveTip, Toast.LENGTH_SHORT).show();
            super.onPostExecute(result);
        }

    }

    /**
     * Cancels the asynchronous work taking place on the ImageView, called by
     * the adapter backing the ViewPager when the child is destroyed.
     */
    public void cancelWork() {
        ImageWorker.cancelWork(ivFrontcover);
        ivFrontcover.setImageDrawable(null);
        ivFrontcover = null;
        tvTitle = null;
        tvActress = null;
        tvIntroduction = null;
        btnDetailShow = null;
        btnDetailHidden = null;
        btnSavePhoto = null;
        ivFrontcover = null;
        layoutInfo = null;
    }

}
